package nc.impl.medpub.bd.hf00101005;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import nc.bs.dao.BaseDAO;
import nc.jdbc.framework.mapping.IMappingMeta;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.trade.sqlutil.IInSqlBatchCallBack;

/**
 * 
 * @author shangjun
 * @since NC5.5
 *
 */
public class MedCustInSqlBatchCallBack implements IInSqlBatchCallBack {
	
	@SuppressWarnings("unchecked")
	private Class voClass;
	private IMappingMeta mapping;
	private String addtionalCondition;
	private HashMap<String,ArrayList<CircularlyAccessibleValueObject>> map=new HashMap<String,ArrayList<CircularlyAccessibleValueObject>>();
    private BaseDAO dao;
	
	

	@SuppressWarnings("unchecked")
	public MedCustInSqlBatchCallBack(Class voClass, String addtionalCondition) {
		super();
		this.voClass = voClass;
		this.addtionalCondition = addtionalCondition;
	}
	
	@SuppressWarnings("unchecked")
	public MedCustInSqlBatchCallBack(Class voClass, IMappingMeta mapping, String addtionalCondition) {
		super();
		this.voClass = voClass;
		this.mapping = mapping;
		this.addtionalCondition = addtionalCondition;
	}


	@SuppressWarnings("unchecked")
	public Object doWithInSql(String inSql) throws BusinessException,
			SQLException {
		String condition="pk_cubasdoc in "+inSql;
		if(addtionalCondition!=null&&addtionalCondition.length()>0){
			condition+=" and ("+addtionalCondition+")";
		}
		Collection c=null;
		if(mapping==null){
			c=getDao().retrieveByClause(voClass, condition);
		}else {
			c=getDao().retrieveByClause(voClass, mapping,condition);
		}
		CircularlyAccessibleValueObject  tempVO=null;
		ArrayList<CircularlyAccessibleValueObject> list=null;
		String pk_cubasdoc=null;
		for(Object vo:c){
			tempVO=(CircularlyAccessibleValueObject)vo;
			pk_cubasdoc=(String)tempVO.getAttributeValue("pk_cubasdoc");
		    list=map.get(pk_cubasdoc);
			if(list==null){
				list=new ArrayList<CircularlyAccessibleValueObject>();
				map.put(pk_cubasdoc, list);
			}
			list.add(tempVO);
		}
		return map;
	}

	public String getAddtionalCondition() {
		return addtionalCondition;
	}

	public void setAddtionalCondition(String addtionalCondition) {
		this.addtionalCondition = addtionalCondition;
	}
	
	public BaseDAO getDao() {
		if(dao==null) {
			dao=new BaseDAO();
			dao.setMaxRows(-1);
		}
		return dao;
	}

}
